package kr.or.javacafe.common.web;

import org.springframework.web.servlet.view.AbstractView;
import org.apache.commons.logging.*;

import java.util.*;

import javax.servlet.http.*;

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

public class JsonViewer extends AbstractView 
{
	protected final static Log logger = LogFactory.getLog(JsonViewer.class);
	
	public void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		if (logger.isDebugEnabled()){logger.debug(getClass() + " : create JSON data.");}
		
		List objList = (java.util.List)model.get("JSON_DATA");
		
		JSONArray jsonArray = JSONArray.fromObject(objList);
		Map map = new HashMap();
		map.put("json_count", jsonArray.size());
		map.put("json_list", jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(map);
		
		// Response contents using mimetype fro JSON.
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		
		response.getWriter().write(jsonObject.toString());
	}
	
}
